<?php
class article extends top
{
	//重定义构造函数
    function __construct(){  
        parent::__construct();
        //定义当前导航
        $this->nav_name = 'article';
        $this->page_title = '文章管理';
        $this->breadcrumb += array('1'=>array('name'=>'文章','url'=>spUrl('article')));

    }
	function index(){	
		$data = $this->spArgs();
		
		//获取当前分类id
		$cat_id = isset($data['cat_id'])?$data['cat_id']:0;
		//构造左侧导航
		$cat_id = $this->__left_nav($cat_id);
		
		if(!is_numeric($cat_id)) $this->__yt_error();
		$this->left_nav_id = $cat_id;
		$this->cat_id =  $cat_id;
		//构造面包屑导航
		$category = spClass("m_category");
		$cat_info  = $category->find(array('id'=>$cat_id));
		if(empty($cat_info)) $cat_info=array('name'=>"全部",'id'=>0);
		$this->breadcrumb += array(
					'2'=>array('name'=>$cat_info['name'],'url'=>spUrl('article','index',array('cat_id'=>$cat_info['id']))),
        			'3'=>array('name'=>'列表')
        		);

		//成功添加、更新文章后的提示
		$notice = array($data['state'],$data['message']);
		
		//更新文章属性
		$article = spClass("m_article");
		if($data['article_update']){
			if(1){
				if(isset($data['weight']))				
					$data_update = array('weight'=>$data['weight']);
				elseif(isset($data['visible']))
					$data_update = array('visible'=>$data['visible']);
				else
					$this->__yt_error();

				if($article->update('id='.$data['article_id'],$data_update)){
					$notice = array('success','更新成功！');
				}else{
					$notice = array('danger','更新出错！');
				}
			}else{
				$notice = array('danger','更新有误！');
			}
		}
		//删除文章
		if($data['delete_article_id']){
			if($article->delete('id='.$data['delete_article_id'])){
				$notice = array('success','删除成功！');
			}else{
				$notice = array('danger','删除出错！');
			}
		}	
		$this->notice = $notice[0]?$notice:null;
		
		//查找文章
		
		if($data['s_submit']){
			$where = "( title like '%" .$data['s_content']. "%' OR content like '%" .$data['s_content']. "%' ) ";
			if($cat_info['id']!=0){	
				$where .= " AND cat_id='".$cat_id."' ";
			}
		}else{
			if($cat_info['id']!=0){
				$where = "cat_id='".$cat_id."'";
			}else{
				$where = "";
			}
		}


		$sort= 'weight DESC,add_time DESC';
		$fields = 'id,cat_id,title,view_num,add_time,visible,weight,pic_cat_id';

		$article_result = $article->spPager($this->spArgs('page', 1), 10)->findAll($where,$sort,$fields);
		$this->pager = $article->spPager()->getPager();
		$this->article_result = $article_result;

		$this->c = 'article';
		$this->a = 'index';		
		$this->data_return = $data;		
	}
	function opera(){
		$data = $this->spArgs();
		//获取当前分类id
		$cat_id = isset($data['cat_id'])?$data['cat_id']:0;
		//构造左侧导航
		$cat_id = $this->__left_nav($cat_id);
		
		if(!is_numeric($cat_id)) $this->__yt_error();
		$this->left_nav_id = $cat_id;
		$this->cat_id =  $cat_id;
		//构造面包屑导航
		$category = spClass("m_category");
		$cat_info  = $category->find(array('id'=>$cat_id));
		if(empty($cat_info)) $cat_info=array('name'=>"全部",'id'=>0);
		$this->breadcrumb += array(
					'2'=>array('name'=>$cat_info['name'],'url'=>spUrl('article','index',array('cat_id'=>$cat_info['id']))),
					'3'=>array('name'=>"添加")
        		);

		//默认为添加文章
		$data_return = array('update_add'=>"添加",'cat_id'=>$cat_id,'add_time'=>date('Y-m-d H:i:s'));
		//更新文章
		$article = spClass("m_article");
		if(is_numeric($data['update_article_id'])){	
			$data_return = $article->find(array('id'=>$data['update_article_id']));
			if(empty($data_return)) $this->__yt_error();
			$data_return['update_add'] = "更新";
			$breadcrumb = $this->breadcrumb;
			$breadcrumb['3'] = array('name'=>$data_return['title']);
			$breadcrumb['4'] = array('name'=>"更新");
			$this->breadcrumb = $breadcrumb;
		}
		//提交文章
		if($data['submit']){
			if(1){
				//匹配文章中的附件
				$data_find = stripcslashes($data['content']);
				preg_match_all("/href=\"?(.*?)kindedit\/attached\/file\/?(.*?)\" target=\"_blank\"\>?(.*?)<\/a/", $data_find, $match);
				foreach($match[0] as $key=>$val){
					$file[$key]= $match[1][$key].'kindedit/attached/file/'.$match[2][$key].'?'. strip_tags($match[3][$key]);
				}
				//是否新建相册
				if($data['pic_cat_id']=='-1'){
					if(trim($data['pic_cat_name'])!=""){
						$pic_cat_id = $category->create(array('module'=>'picture','father_id'=>0,'name'=>$data['pic_cat_name']));
						$data['pic_cat_id']=$pic_cat_id;
					}else{
						$data['pic_cat_id']='0';
					}
				}
				//var_dump($data);break;
				$data_insert = array(
					'editor_id'=>$_SESSION['ID'],
					'cat_id'=>$data['cat_id'],
					'title'=>$data['title'],
					'img_dir'=>$data['img_dir']?$data['img_dir']:0,
					'content'=>$data['content'],
					'description'=>$data['description'],
					'add_time'=>$data['add_time'],
					'file' => implode('|',$file),
					'pic_cat_id' => $data['pic_cat_id']
				);

				if($data['id']){
					$is_ok = $article->update('id='.$data['id'],$data_insert);
				}else{
					$is_ok = $article->create($data_insert);
				}
				if($is_ok){
					$this->jump(spUrl('article', 'index',array('cat_id'=>$data['cat_id'],'state'=>'success','message'=>$data['update_add'].'成功！','content_only'=>$this->content_only))); 
				}else{
					$data_return = $data;
					$this->notice = array('danger',$data['update_add'].'出错！');
				}
			}else{
				$data_return = $data;
				$this->notice = array('danger','填写有误！');				
			}
		}
		$this->pic_cat_result = $category->findAll(array('module'=>'picture','father_id'=>0),'weight DESC');
		$this->data_return = $data_return;
	}	
}